import { ref, computed } from "vue";
import { defineStore } from "pinia";

export const useCityStore = defineStore("city", () => {
  /* 3、改变city这个默认值，先判断缓存中是否有值，有值就给city.value */
  const getDefaultCity = () => {
    let city = ref("北京");
    let currentCity = localStorage.getItem("currentCity");
    if (currentCity) {
      //如果缓存中有值，就改变city的默认值
      city.value = currentCity;
    }
    return city;
  };
  /* 1 、设置默认位置为北京 */
  const city = getDefaultCity(); //4、将默认值北京const city = ref("北京")  改为 上面的getDefauktCity()函数
  const changeCity = (val) => {
    city.value = val;
    /* 2、设置缓存 */
    localStorage.setItem("currentCity", val);
  };
  return {
    city,
    changeCity,
  };
});